const mongoose = require("mongoose");
const moment = require('moment');
const Schema = mongoose.Schema;
const {
  v4: uuidv4
} = require('uuid');
const expiresTime = require('../config/keys').expiresTime
const {timeZone}  = require('@michaelray/mongoose-timezone');
/**
 * 用户注册
 */
const userSchema = new Schema({
  nickName: {
    type: String,
  },
  userName: {
    type: String,
    required: true,
    index: true,
    unique: true
  },
  passWord: {
    type: String,
    required: true,
  },
  avatar: {
    type: String,
  },
  status: {
    type: String,
  },
  delFlag: {
    type: Boolean,
  },
  roleIds: {
    type: Array,
  },
  stationId: {
    type: String,
  },
  createTime: {
    type: String,
  },
  updateTime: {
    type: String,
  }
}, {
  timestamps: {
    createdAt: 'createTime',
    updatedAt: 'updateTime',
    currentTime: () => moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
  }
});

const Users = mongoose.model("users", userSchema);

/**
 * 用户--角色关联表
 */
const userRelatRoleSchema = new Schema({
  userName: String,
  roleId: String,
})

const UserRelatRoles = mongoose.model("user_relat_roles", userRelatRoleSchema);

/**
 * Token生成
 */
const tokenSchema = new Schema({
  token: {
    type: String,
    required: true,
  },
  expiresIn: {
    type: String,
    required: true,
  },
  userName: {
    type: String,
    required: true,
  },
  createTime: String,
  updateTime: String,
  expiresDate: {
    type: Date,
    default: new Date(),
    expireAfterSeconds: 10,
    index: true,
  },
}, {
  timestamps: {
    createdAt: 'createTime',
    updatedAt: 'updateTime',
    currentTime: () => moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
  }
});
// tokenSchema.index({
//   expiresDate: 1,
// }, {
//   expireAfterSeconds: 28800
// })
// tokenSchema.plugin(timeZone, { paths: ['expiresDate']});
const Tokens = mongoose.model("tokens", tokenSchema);

/**
 * 菜单
 */
const MenuSchema = new Schema({
  menuName: String,
  menuIcon: String,
  menuType: String,
  parentId: String,
  routeName: {
    type: String,
    required: true,
    index: true,
    unique: true
  },
  routePath: String,
  menuState: Number,
  menuSort: Number,
  clientId: String,
  deleted: Number,
  createBy: String,
  createTime: String,
  updateTime: String,
}, {
  timestamps: {
    createdAt: 'createTime',
    updatedAt: 'updateTime',
    currentTime: () => moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
  }
})

const MenusLists = mongoose.model("sys_menus", MenuSchema)

/**
 * 页面按钮权限
 */
const AuthSchema = new Schema({
  authName: String,
  authValue: String,
  menuId: String,
  clientId: String,
})

const AuthLists = mongoose.model("sys_auths", AuthSchema)


/**
 * 角色
 */
const RoleSchema = new Schema({
  id: {
    type: String,
    default: uuidv4(),
    unique: true,
    index: true
  },
  roleName: String,
  roleDesc: String,
  createTime: String,
  updateTime: String,
}, {
  timestamps: {
    createdAt: 'createTime',
    updatedAt: 'updateTime',
    currentTime: () => moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
  }
})

const RoleLists = mongoose.model("sys_roles", RoleSchema)

/**
 * 角色关联菜单
 */
const RoleRelatMenuSchema = new Schema({
  roleId: String,
  menuId: String,
})

const RoleRelatMenu = mongoose.model("role_relat_menus", RoleRelatMenuSchema)

/**
 * 角色关联数据权限
 */
const RoleRelatDataAuthSchema = new Schema({
  roleId: String,
  dataAuthId: String,
  defaultflag: Boolean
})

const RoleRelatDataAuth = mongoose.model("role_relat_data_auths", RoleRelatDataAuthSchema)

/**
 * 角色关联操作权限
 */
const RoleRelatHandleAuthSchema = new Schema({
  roleId: String,
  authId: String,
})

const RoleRelatHandleAuth = mongoose.model("role_relat_handle_auths", RoleRelatHandleAuthSchema)


/**
 * 应用
 */
const AppSchema = new Schema({
  sysName: String,
  value: String,
  appUrl: String,
  createTime: String,
  updateTime: String,
}, {
  timestamps: {
    createdAt: 'createTime',
    updatedAt: 'updateTime',
    currentTime: () => moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
  },
})

const AppLists = mongoose.model("sys_apps", AppSchema)

/**
 * 整体导出
 */
module.exports = {
  Users,
  Tokens,
  // UserInfos,
  UserRelatRoles,
  MenusLists,
  AuthLists,
  RoleLists,
  RoleRelatMenu,
  RoleRelatDataAuth,
  RoleRelatHandleAuth,
  AppLists
};